#!/bin/sh

# This script must run on a lot of different platforms.
# It assumes that the following things are installed:
# * curl
# * jq
# * cat

# We do not set -x because this would leak the oauth access token.
set +x

set -e

VERSION="$1"
OAUTH_TOKEN="$2"
YML_FILE="tmp.yml"

if [ -z "$VERSION" ]; then
  echo "missing VERSION parameter"
  exit 1
fi

if [ -z "$OAUTH_TOKEN" ]; then
  echo "missing OAUTH_TOKEN parameter"
  exit 1
fi

cat <<EOF >"$YML_FILE"
image: alpine/latest
packages:
  - py3-pip
  - curl
  - jq
  - xz
secrets:
  - 51bfddf5-86a6-4e01-8576-358c72a4a0a4
  - 44e2bd57-1d07-42bf-925e-22a36119041d
sources:
  - https://github.com/ziglang/zig
tasks:
  - build: cd zig && ./ci/srht/update_download_page $VERSION
EOF

jq <$YML_FILE -sR '{
      "manifest": .,
    }' | curl \
  -H Authorization:"token $OAUTH_TOKEN" \
  -H Content-Type:application/json \
  -X POST \
  -d @- "https://builds.hut.lavatech.top/api/jobs"
